'''
所有章节名称(章节名, cid)  === 同步
https://dushu.baidu.com/api/pc/getCatalog?data={"book_id":"4305597289"}

章节内容  === 异步
https://dushu.baidu.com/api/pc/getChapterContent?data={"book_id":"4305597289","cid":"4305597289|10409397","need_bookinfo":1}
'''
import requests
import asyncio
import aiohttp
import aiofiles
import json

'''
分析
1. 同步: 访问getCatalog ==> 所有章节名和 cid
2. 异步: 访问getChapterContent ==> 所有章节的内容
'''

# 获取章节内容
async def aio_download(cid, title, b_id):
    data = {
        "book_id": b_id,
        "cid": f"{b_id}|{cid}",
        "need_bookinfo": 1
    }
    data = json.dumps(data)
    url = f"https://dushu.baidu.com/api/pc/getChapterContent?data={data}"

    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            content = await resp.json()

            async with aiofiles.open(f"novel/{title}", mode="w", encoding="utf-8") as f:
                await f.write(title + "\n")
                await f.write(content["data"]["novel"]["content"])


# 获取所有章节名和cid
async def getChapter(url, head):
    resp = requests.get(url, headers=head)
    # 获取所有章节信息
    chapters = resp.json()["data"]["novel"]["items"]
    # 保存异步任务
    tasks = []
    # item 表示每个章节的信息
    for item in chapters:
        title = item["title"]
        cid = item["cid"]
        # 准备异步任务
        tasks.append(asyncio.create_task(aio_download(cid, title, b_id)))

    await asyncio.wait(tasks)




if __name__ == '__main__':
    b_id = "4305597289"
    url = 'https://dushu.baidu.com/api/pc/getCatalog?data={"book_id":"' + b_id + '"}'
    head = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
    }
    asyncio.run(getChapter(url, head))
    print("over")

